package com.teacher.server;

import com.teacher.entity.Student;
import com.teacher.entity.Teacher;
import com.teacher.util.Datas;
import com.teacher.util.SysConstants;
import com.teacher.util.Tools;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

/**
 * ServerContextListener--bdqnPractice
 *
 * @author: Jovi
 * @createTime: 2017-09-25 16:22
 **/
public class ServerContextListener extends Thread {

    public static void main(String[] args) {

        new ServerContextListener().start();
    }

    @Override
    public void run() {

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ObjectInputStream ois = null;
        OutputStream os = null;
        ObjectOutputStream oos = null;

        try {
            String value = Tools.getValue(SysConstants.SYS_LISTENERPORT.getName());
            ss = new ServerSocket(Integer.parseInt(value));
            while (true) {
                socket = ss.accept();

                is = socket.getInputStream();
                ois = new ObjectInputStream(is);

                os = socket.getOutputStream();
                oos = new ObjectOutputStream(os);

                //接收客户端请求对象
                Datas datas = (Datas) ois.readObject();

                //创建服务器的业务对象
                ServerService serverService = new ServerService();

                if (datas.getFlag().equals(SysConstants.SYS_LOGIN.getName())) {

                    //登录
                    String userName = datas.getTeacher().getUserName();
                    String passWord = datas.getTeacher().getPassWord();

                    Teacher teacher = serverService.doLogin(userName, passWord);
                    if (teacher == null) {
                        datas.setFlag(SysConstants.SYS_ERROR.getName());
                    } else {
                        datas.setFlag(SysConstants.SYS_SUCCESS.getName());
                        datas.setTeacher(teacher);//TODO 可以不用设值
                    }
                } else {
                    if (SysConstants.SYS_FIND.getName().equals(datas.getFlag())) {

                        int teacherId = datas.getTeacher().getId();
                        List<Student> students = serverService.findStudentByTeacher(teacherId);
                        datas.setStudents(students);

                    } else if (SysConstants.SYS_UPDATE.getName().equals(datas.getFlag())) {

                        Student student = datas.getStudent();
                        serverService.updateStudent(student);

                    } else if (SysConstants.SYS_DELETE.getName().equals(datas.getFlag())) {
                        int stuId = datas.getStudent().getStuId();
                        serverService.deleteStudent(stuId);

                    }
                    datas.setFlag(SysConstants.SYS_SUCCESS.getName());
                }
                oos.writeObject(datas);
                oos.flush();
                socket.shutdownOutput();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null)
                    oos.close();
                if (os != null)
                    os.close();
                if (ois != null)
                    ois.close();
                if (is != null)
                    is.close();
                if (socket != null)
                    socket.close();
                if (ss != null)
                    ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("服务器终止");
    }
}
